package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.basic.TerminalUpgradePack;
import cc.yugu.yhdw.webserver.persistence.basic.TerminalUpgradePackMapper;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 终端升级业务切面类
 * 
 * @Date:Created in 2018/7/5 15:28
 * @Author:
 * @Modified By: zjn 2018/7/5 15:28
 */ 
@Component
@Aspect
public class TerminalUpgradePackServiceAspect {

    @Autowired
    TerminalUpgradePackMapper terminalUpgradePackMapper;
    @Autowired
    MemCachedClient memCachedClient;

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.TerminalUpgradePackService.update(..))")
    private void update() {}
    /**
    * 删除旧版本号
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/5  15:28
    */
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取目标方法的 入口参数
        TerminalUpgradePack record = (TerminalUpgradePack) joinPoint.getArgs()[0];
        // 按照指定格式（"45:%d:%d:%s"）输出 运营商id和旧版本号
        String key = CacheKey.key(CacheKey.K_AGENT_ID_MODULE_ID_OLD_VERSION_V_TERMINAL_UPGRADE_PACK, record.getAgentId(),record.getOldVersion());
        memCachedClient.delete(key);

        return joinPoint.proceed();
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.TerminalUpgradePackService.delete(..))")
    private void delete() {}
    /**
    * 删除
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/5  15:38
    */
    @Around("delete()")
    public Object delete(ProceedingJoinPoint joinPoint) throws Throwable {
        Integer id = (Integer) joinPoint.getArgs()[0];


        TerminalUpgradePack line = terminalUpgradePackMapper.find(id);
        if(line != null) {
            String key = CacheKey.key(CacheKey.K_AGENT_ID_MODULE_ID_OLD_VERSION_V_TERMINAL_UPGRADE_PACK, line.getAgentId(),line.getOldVersion());
            memCachedClient.delete(key);
        }

        Object result = joinPoint.proceed();
        return result;
    }
}
